package relu.fbs.jfm;

import java.util.Map;

import jfm.model.Types.CropType;
import relu.fbs.DataValue;
import relu.fbs.jfm.JFMFarmData.JFMFarmDatum;

public class JFMFarmDataTSAv extends AbstractJFMFarmData {
	private int numInAverage = 0;
	public int numInAverage(){return numInAverage;};
	/** Create a new average using the first JFMFarmData as a template 
	 * also adds the first JFMFarmData to the average */
	public JFMFarmDataTSAv(JFMFarmData first){
		super(first.farmId);
		for(JFMFarmDatum d:JFMFarmDatum.values()){
			DataValue dv=new DataValue(true);
			dv.add(first.farmData.get(d).autoValue());
			farmData.put(d, dv); // All intensive values 
		}
		for(AbstractJFMCropData cp:first.crops.values()){
			
			JFMCropDataTSAv cv=new JFMCropDataTSAv((JFMCropData)cp);
			crops.put(cp.cropType, cv);
		}
		numInAverage++;
	}
	
	
	/** Add a JFMFarmData object to the time series average */
	void increment(JFMFarmData next){
		// Increment farmData entries
		for(Map.Entry<JFMFarmDatum, DataValue> ent :farmData.entrySet()){
			if ( ent.getKey().isStatic){
				if ( ent.getValue().autoValue()!=next.farmData.get(ent.getKey()).autoValue()){
					String message = "Static entries should remain constant for same farm. \n   - Offending Entry "+
							ent.getKey()+" v: "+ent.getValue().autoValue()+" vs "+next.farmData.get(ent.getKey()).autoValue()+" for farm "+farmId;
//					System.out.println("WARNING --- "+message);
				}
			} else {
				ent.getValue().add(next.farmData.get(ent.getKey()).autoValue());
			}
		}
		for(Map.Entry<CropType, AbstractJFMCropData> ent:crops.entrySet()){
			JFMCropDataTSAv val=(JFMCropDataTSAv)ent.getValue();
			val.increment((JFMCropData)next.crops.get(ent.getKey()));
		}		
		numInAverage++;	
	}
			
}
